package me.shibor.study.quartz.tutorials;

import java.util.ArrayList;
import java.util.Date;

import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;

import me.shibor.study.quartz.job.DumbJob;

public class Lesson3 {

	public static void main(String[] args) throws SchedulerException {

		SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();

		Scheduler sched = schedFact.getScheduler();

		sched.start();

		// define the job and tie it to our DumbJob class
		JobDetail job = JobBuilder.newJob(DumbJob.class).withIdentity("myJob", "group1")
				.usingJobData("jobSays", "Hello World!")
				.usingJobData("myFloatValue", 3.141f)
				.usingJobData("test", 123)
				.usingJobData("type", "jobDetails")
				.build();
		JobDataMap jobDataMap = job.getJobDataMap();
		ArrayList<Date> state = new ArrayList<>();
		state.add(new Date());
		jobDataMap.put("myStateData", state);
		
		// Trigger the job to run now, and then every 40 seconds
		Trigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger", "group1").startNow()
				.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(10).repeatForever()).build();
		
		JobDataMap jobDataMap2 = trigger.getJobDataMap();
		jobDataMap2.put("type", "trigger");

		// Tell quartz to schedule the job using our trigger
		sched.scheduleJob(job, trigger);

		// sched.shutdown();

		// sched.start();//The Scheduler cannot be restarted after shutdown()
		// has been called.
	}

}
